<?php

namespace IDock\Insurance\AICS\Response;

use IDock\Http\Response;
use IDock\IDock\IDockEnums;
use IDock\Insurance\AICS\Response\AllAICSResponse;
use IDock\Printer\NonCar\NonCarInquiryPrinter;
use IDock\Printer\NonCar\NonCarInquirySinglePrinter;
use IDock\Printer\PolicyPaymentPrinter;
use IDock\Printer\ProposalStatusPrinter;
use IDock\Visitor\NonCar\NonCarProductVisitor;

class PolicyInfoResponse extends AllAICSResponse
{

    public function resolve(): Response
    {
        $body = $this->getBody();
        $body = str_replace('&lt;','<',$body);
        $body = str_replace('&gt;','>',$body);
        $body = str_replace("'",'"',$body);
        $start = strpos($body,'<return>');
        $end = strpos($body,'</return>');
        $body = substr($body,$start + 8,intval($end - $start -8));
        if ($body) {
            $str = base64_decode(base64_decode($body));
            parse_str($str,$arr);
            $body = $arr;
            $errorCode = $body['result'] ?? '';
            $message = $body['message'] ?? '未知错误';
            $this->setBody($body);
            if ($errorCode == '1') {
                $this->setError($message);
                return $this;
            }
        }

        return $this;
    }

    public function printerInit(): Response
    {
        $data = $this->getBody();
        $proposalStatusPrinter = new ProposalStatusPrinter();
        $proposalStatusPrinter->ciInquiryNo = $data['ci_policy_app_no'] ?? '';
        $proposalStatusPrinter->ciPolicyNo = $data['ci_policy_no'] ?? '';
        $proposalStatusPrinter->biInquiryNo = $data['biz_policy_app_no'] ?? '';
        $proposalStatusPrinter->biPolicyNo = $data['biz_policy_no'] ?? '';
        $ciStatus = $data['jqstatus'] ?? '';
        $biStatus = $data['systatus'] ?? '';
        $status = '0';
        $jqMsg = $data['jqmessage'] ?? '';
        $syMsg = $data['symessage'] ?? '';
        if($ciStatus !== '' && $biStatus !== ''){
            if(in_array($ciStatus,['0','5']) or in_array($biStatus,['0','5'])){
                $status = '5';//人工核保
            }
            elseif ($ciStatus == '3' or $biStatus == '3'){
                $status = '3';//退回修改
                if(!empty($jqMsg) or !empty($syMsg)){
                    $proposalStatusPrinter->messageTip = implode(' ',[$jqMsg,$syMsg]);
                }

            }
            elseif ($ciStatus == '9' && $biStatus == '9'){
                $status = '9';//核保通过
            }
            elseif ($ciStatus == '1' && $biStatus == '1'){
                $status = '1';//已出保单
            }

        }else{
            $status = $ciStatus ?: $biStatus;
        }
        //非车部分
        if(isset($data['yw_policy_app_no'])){
            $nonCarInquiryPrinter = new NonCarInquiryPrinter();
            /*** @var $item NonCarProductVisitor */
            $single = new NonCarInquirySinglePrinter();
            $single->inquiryNo = $data['yw_policy_app_no'];
            $single->policyNo = $data['yw_policy_no'];
            $single->statusCode = 'success';
            $nonCarInquiryPrinter->noCarInquirySingleAppend($single);
            $proposalStatusPrinter->nonCarInquiryPrinter = $nonCarInquiryPrinter;
        }
        $proposalStatusPrinter->statusCode = $this->getStatus($status);
        $proposalStatusPrinter->status = IDockEnums::$underStats[$proposalStatusPrinter->statusCode] ?? '';
        $this->printer = $proposalStatusPrinter;
        return  $this;
    }
    private function getStatus($status)
    {
        $u = 'U3';
        switch ($status){
            case '5':
            case '0':
                $u = 'U3';
                break;
            case '9':
                $u = 'U1';
                break;
            case '3':
                $u = 'U2';
                break;
            case '1':
                $u = 'U5';
                break;
        }
        return $u;
    }
}
